package com.zetyun.tiger.format.date;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Calendar;
import java.util.Date;

public class SpecialDateFormatUtil {


    private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static final DateTimeFormatter dtfFull = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static final DateTimeFormatter eibocAllFull = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
    private static final DateTimeFormatter epbAllFull = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
    private static final DateTimeFormatter cmtxlFull = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
    private static final DateTimeFormatter epayAllFull = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");

    public static Long getRecordTs(String timeStr) {
        return generateRecordCalendar(timeStr).getTimeInMillis();
    }

    public static Calendar generateRecordCalendar(String timeStr) {
        String year = "20".concat(timeStr.substring(1, 3));
        String days = timeStr.substring(3, 6);
        String hour = timeStr.substring(6, 8);
        String minute = timeStr.substring(8, 10);
        String second = timeStr.substring(10, 12);
        String millSec = timeStr.substring(12, 15);

        return new Calendar.Builder()
                .set(Calendar.YEAR, Integer.parseInt(year))
                .set(Calendar.DAY_OF_YEAR, Integer.parseInt(days))
                .set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour))
                .set(Calendar.MINUTE, Integer.parseInt(minute))
                .set(Calendar.SECOND, Integer.parseInt(second))
                .set(Calendar.MILLISECOND, Integer.parseInt(millSec))
                .build();

    }


    public static Long toTs(String dtStr, boolean isFull) {
        LocalDateTime localDateTime = null;
        if (!isFull) {
            dtStr = dtStr + " 00:00:00";
        }
        localDateTime = LocalDateTime.parse(dtStr, dtfFull);

        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    public static Long toEibocTs(String dtStr) {
        LocalDateTime localDateTime = LocalDateTime.parse(dtStr, eibocAllFull);
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    public static Long toEpayTs(String dtStr) {
        LocalDateTime localDateTime = LocalDateTime.parse(dtStr, epayAllFull);
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    public static Long toEpbTs(String dtStr) {
        String ymdHms = dtStr.substring(0, 14);
        Long tm = toEpbTsInternal(ymdHms);
        int ms = Integer.parseInt(dtStr.substring(14, 17));
        return tm + ms;
    }

    public static Long toCmtxlTs(String dateStr, String timeStr) {
        return toEpbTsInternal(dateStr + timeStr.substring(0,6));
    }

    private static Long toEpbTsInternal(String ymdHms) {
        LocalDateTime localDateTime = LocalDateTime.parse(ymdHms, epbAllFull);
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    public static Long toGbismTs(String date, String trxTime, Object endTime) {
        if (endTime != null && !endTime.toString().trim().isEmpty()) {
            return toEpbTsInternal(date + endTime);
        } else {
            return toEpbTsInternal(date + trxTime);
        }
    }

    public static Long toTs(String dtStr) {
        return toTs(dtStr, false);
    }

    public static String toDate(Long ts) {
        Date dt = new Date(ts);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(dt.toInstant(), ZoneId.systemDefault());
        return dtf.format(localDateTime);
    }

    public static String toYmdHms(Long ts) {
        Date dt = new Date(ts);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(dt.toInstant(), ZoneId.systemDefault());
        return dtfFull.format(localDateTime);
    }

    public static String day(String ymdHms) {
//        String year = ymdHms.substring(0, 4);
//        String month = ymdHms.substring(5, 7);
//        String day = ymdHms.substring(8, 10);
//        return year + month + day;
        return ymdHms.substring(0, 10);
    }

    public static String hour(String ymdHms) {
        return ymdHms.substring(11, 13);
    }

    public static String minute(String ymdHms) {
        return ymdHms.substring(14, 16);
    }

    public static void main(String[] args) {
//        Long aLong = toEpayTs("2020-11-04 08:21:26.040");
//        String s = toYmdHms(aLong);
//        System.out.println(aLong);
//        System.out.println(s);

        Long ts = toCmtxlTs("20221009", "173030999");
        String s = toYmdHms(ts);
        System.out.println(s);
        System.out.println(day(s));
    }
}
